<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );

/**
 * Description of Users
 *
 * @author doufu <442958506@qq.com>
 */
class Users extends REST_Controller {
	
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'api/User_model', 'users' );
		$this->load->library ( 'form_validation' );
	}
	
	/**
	 * 短信验证
	 */
	public function sms_verify() {
		$this->form_validation->validateData ( array (
				'username',
				'code' 
		), array (
				array (
						'username',
						'mobile' 
				),
				array (
						'code',
						'ingeter' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		
		$this->load->library ( 'vendor_interface' );
		
		$result = $this->vendor_interface->verify ( $this->form_validation->username, $this->form_validation->code );
		if ($result) {
			$this->renderJson ( 11100 );
		} else {
			$this->renderJson ( 11200 );
		}
	}
	
	/**
	 * 注册
	 */
	public function signup() {
		$this->form_validation->validateData ( array (
				'username',
				'password',
				'code' 
		), array (
				array (
						'username',
						'mobile|is_unique[user.username]' 
				),
				array (
						'password',
						'required|min_length[6]' 
				),
				array (
						'code',
						'required|integer' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		
		$this->load->library ( 'vendor_interface' );
		$ret = $this->vendor_interface->verify ( $this->form_validation->username, $this->form_validation->code );
		if (! $ret) {
			$this->renderJson ( 10101 );
		}
		
		$this->load->library ( 'session' );
		$this->session->sess_regenerate ( TRUE );
		$token = session_id ();
		$dataInsert = array (
				'username' => $this->form_validation->username,
				'password_hash' => md5 ( md5 ( $this->form_validation->password ) ),
				'status' => parent::STATUS_ACTIVE,
				'access_token' => $token,
				'created_at' => time (),
				'updated_at' => time () 
		);
		$result = $this->users->insert ( $dataInsert );
		if ($result) {
			$SESS = array (
					'uid' => $result,
					'username' => $dataInsert ['username'],
					'role' => parent::ROLE_USER,
					'updated_at' => $dataInsert ['updated_at'],
					'status' => parent::STATUS_ACTIVE,
					parent::__ID => parent::__ID_PARAM 
			);
			$this->session->set_userdata ( $SESS );
			session_write_close ();
			$data ['nickname'] = '';
			$data ['username'] = preg_replace ( '/(1\d{2})\d{4}(\d*)/i', '$1***$2', $SESS ['username'] );
			$data ['user_head_img'] = base_url ( 'data/share/upload/head_img/14405938061321.jpg' );
			$data [config_item ( 'user.tokenParam' )] = $token . '.' . $SESS ['updated_at'];
			$this->renderJson ( 11100, array (
					'data' => $data 
			) );
		} else {
			$this->renderJson ( 11201 );
		}
	}
	
	/**
	 * 登陆
	 */
	public function login() {
		$this->form_validation->validateData ( array (
				'username',
				'password' 
		), array (
				array (
						'username',
						'mobile' 
				),
				array (
						'password',
						'required|min_length[6]|validate_password[user.username]' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		
		if (! empty ( $this->form_validation->user->access_token )) {
			session_id ( $this->form_validation->user->access_token );
		}
		$this->load->library ( 'session' );
		$this->session->sess_regenerate ( TRUE );
		$token = session_id ();
		$dataUpdate = array (
				'access_token' => $token,
				'updated_at' => time () 
		);
		
		$result = $this->users->update ( $dataUpdate, array (
				'id' => $this->form_validation->user->id 
		) );
		if ($result) {
			$SESS = array (
					'uid' => $this->form_validation->user->id,
					'username' => $this->form_validation->username,
					'role' => parent::ROLE_USER,
					'updated_at' => $dataUpdate ['updated_at'],
					'status' => parent::STATUS_ACTIVE,
					parent::__ID => parent::__ID_PARAM 
			);
			$this->session->set_userdata ( $SESS );
			session_write_close ();
			$data ['id'] = $SESS ['uid'];
			$data ['nickname'] = $this->form_validation->user->nickname;
			$data ['username'] = preg_replace ( '/(1\d{2})\d{4}(\d*)/i', '$1***$2', $SESS ['username'] );
			$data ['user_head_img'] = base_url ( 'data/share/upload/head_img/14405938061321.jpg' );
			$data ['address_id'] = $this->form_validation->user->address_id;
			$this->load->model ( 'api/Areas_model', 'Areas' );
			$data ['address'] = $this->Areas->getAddressName ( $data ['address_id'] );
			$data [config_item ( 'user.tokenParam' )] = $token . '.' . $SESS ['updated_at'];
			$this->renderJson ( 11100, array (
					'data' => $data 
			) );
		} else {
			$this->renderJson ( 11201 );
		}
	}
	
	/**
	 * 忘记密码
	 */
	public function forget() {
		$this->form_validation->validateData ( array (
				'username',
				'code',
				'newPassword' 
		), array (
				array (
						'username',
						'mobile' 
				),
				array (
						'code',
						'required|integer' 
				),
				array (
						'newPassword',
						'required|min_length[6]' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		
		$this->load->library ( 'vendor_interface' );
		$ret = $this->vendor_interface->verify ( $this->form_validation->username, $this->form_validation->code );
		if (! $ret) {
			$this->renderJson ( 10101 );
		}
		
		$data = array (
				'password_hash' => md5 ( md5 ( $this->form_validation->newPassword ) ) 
		);
		$result = $this->users->update ( $data, array (
				'username' => $this->form_validation->username 
		) );
		if ($result) {
			$this->renderJson ( 11100 );
		}
		$this->renderJson ( 11200 );
	}
	
	/**
	 * 热销商品
	 */
	public function hot() {
		$this->form_validation->validateData ( array (
				'address_id' 
		), array (
				array (
						'address_id',
						'required|integer' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		$address_id = $this->form_validation->address_id;
		
		// 获取小仓库拥有的所有商品
		$this->load->model ( 'api/Product_model', 'Product' );
		$pList = $this->Product->getProduct ( $address_id, true );
		$data = array (
				'data' => array () 
		);
		if (! empty ( $pList )) {
			$tmp = array_slice ( $pList, $this->page ['offset'], $this->page ['limit'] );
			$data ['data'] = $tmp;
		}
		$this->renderJson ( 11100, $data );
	}
	public function product() {
		$this->form_validation->validateData ( array (
				'address_id' 
		), array (
				array (
						'address_id',
						'required|integer' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		$this->load->model ( 'api/Category_model', 'Category' );
		$category = $this->Category->getCategoryAll ();
		$this->load->model ( 'api/Product_model', 'Product' );
		$pList = $this->Product->getProduct ( $this->form_validation->address_id, false );
		$hotId = false;
		$data = array ();
		$hot = array ();
		if (! empty ( $category )) {
			foreach ( $category as $v ) {
				$data [$v ['id']] = $v;
				switch ($v ['type']) {
					case '1' :
						$hotId = $v ['id'];
						break;
					default :
						break;
				}
			}
		}
		if (! empty ( $pList )) {
			foreach ( $pList as $v ) {
				$id = $v ['category_id'];
				if (isset ( $data [$id] )) {
					$data [$id] ['product'] [] = $v;
				}
				if ($v ['isHot']) {
					$hot [] = $v;
				}
			}
		}
		if ($hotId) {
			$data [$hotId] ['product'] = $hot;
		}
		$this->renderJson ( 11100, array (
				'data' => array_values ( $data ) 
		) );
	}
	
	/**
	 * 获取所有分类
	 */
	public function getCategory() {
		$this->load->model ( 'api/Category_model', 'Category' );
		$data ['data'] = $this->Category->getCategoryAll ();
		$this->renderJson ( 11100, $data );
	}
	
	/**
	 * 获取banner
	 */
	public function getBanner() {
		$this->load->model ( 'api/Album_model', 'Album' );
		$data ['data'] = $this->Album->getAlbumAll ( '3', '2' ); // 来源banner,类型为链接
		$this->renderJson ( 11100, $data );
	}
	
	/**
	 * 获取启用城市
	 */
	public function getCities() {
		$this->form_validation->validateData ( array (
				'lng',
				'lat' 
		), array (
				array (
						'lng',
						'required|numeric' 
				),
				array (
						'lat',
						'required|numeric' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		$this->load->library ( 'vendor_interface' );
		$ad_info = $this->vendor_interface->getLocationFromQQ ( floatval ( $this->form_validation->lng ), floatval ( $this->form_validation->lat ) );
		$position = array (
				'province_id' => (floor ( $ad_info ['adcode'] / 10000 ) * 10000),
				'city_id' => (floor ( $ad_info ['adcode'] / 100 ) * 100),
				'district_id' => $ad_info ['adcode'] 
		);
		$this->load->model ( 'api/Areas_model', 'Areas' );
		$areas = $this->Areas->getAreasAll ();
		if (isset ( $areas [$position ['province_id']] )) {
			$tmp = $areas [$position ['province_id']];
			unset ( $areas [$position ['province_id']] );
			if (isset ( $tmp ['city'] [$position ['city_id']] )) {
				$tmp1 = $tmp ['city'] [$position ['city_id']];
				unset ( $tmp ['city'] [$position ['city_id']] );
				if (isset ( $tmp1 ['district'] [$position ['district_id']] )) {
					$tmp2 = $tmp1 ['district'] [$position ['district_id']];
					unset ( $tmp1 ['district'] [$position ['district_id']] );
					array_unshift ( $tmp1 ['district'], $tmp2 );
				}
				array_unshift ( $tmp ['city'], $tmp1 );
			}
			array_unshift ( $areas, $tmp );
		}
		$areas = array_values ( $areas );
		if (! empty ( $areas )) {
			foreach ( $areas as &$value ) {
				foreach ( $value ['city'] as &$val ) {
					foreach ( $val ['district'] as &$v ) {
						$v ['areas'] = array_values ( $v ['areas'] );
					}
					$val ['district'] = array_values ( $val ['district'] );
				}
				$value ['city'] = array_values ( $value ['city'] );
			}
		}
		$data ['data'] = $areas;
		return $this->renderJson ( 11100, $data );
	}
	
	/**
	 * 获取启用地区
	 */
	public function getAreas() {
		$this->form_validation->validateData ( array (
				'city_id' 
		), array (
				array (
						'city_id',
						'required|integer' 
				) 
		) );
		
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		$this->load->model ( 'api/Areas_model', 'Areas' );
		$areas = $this->Areas->getAreasAll ();
		$city_id = $this->form_validation->city_id;
		if (! empty ( $areas ) && isset ( $areas [$city_id] )) {
			$tmp = array (
					'city_id' => $city_id,
					'city_name' => $areas [$city_id] ['city_name'],
					'district' => array_values ( $areas [$city_id] ['district'] ) 
			);
			$data ['data'] = $tmp;
			$this->renderJson ( 11100, $data );
		}
		$this->renderJson ( 11100 );
	}
	
	/**
	 * android版本
	 */
	public function version() {
		$this->load->config ( 'system_info' );
		$data = array (
				'data' => array (
						'versioncode' => intval ( config_item ( 'cfg_android_version' ) ),
						'downloadUrl' => config_item ( 'cfg_android_download' ),
						'desc' => config_item ( 'cfg_android_update_desc' ) 
				) 
		);
		$this->renderJson ( 11100, $data );
	}
	
	/**
	 * 联系我们
	 */
	public function contactUs() {
		$this->load->config ( 'system_info' );
		$data = array (
				'company_name' => config_item ( 'cfg_company' ),
				'company_address' => config_item ( 'cfg_company_address' ),
				'beian' => config_item ( 'cfg_beian' ),
				'tel' => config_item ( 'cfg_tel' ),
				'company_desc' => config_item ( 'cfg_company_desc' ) 
		);
		$this->renderJson ( 11100, array (
				'data' => $data 
		) );
	}
	public function test() {
		$this->load->library ( 'JPush' );
		$result = $this->jpush->push ( array (
				75 
		), 'cfg_agent_notify_cover_c', NULL, array (
				'data' => '68' 
		) );
		var_dump ( $result );
	}
	
	/**
	 * 用户获取公告
	 */
	public function getNotice() {
		$this->form_validation->validateData ( array (
				'address_id' 
		), array (
				array (
						'address_id',
						'required|integer' 
				) 
		) );
		if ($this->form_validation->hasErrors ()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson ( 10005, $data );
		}
		$address_id = $this->form_validation->address_id;
		
		$this->load->model ( 'api/Areas_model', 'Areas' );
		$data ['data'] = $this->Areas->findAreas ( $address_id, 'notice' );
		$this->renderJson ( 11100, $data );
	}
}
